package com.example.wifi_direct.utils

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.util.Log
import org.jetbrains.anko.db.ManagedSQLiteOpenHelper

class SigninDataBase (var context: Context): ManagedSQLiteOpenHelper(context,"SiginDataBase.db",null) {

    companion object {
        var DB_NAME: String = "SiginDataBase.db"

        var TABLE_NAME = "Sign_Log"

        private var instance: SigninDataBase? = null

        @Synchronized
        fun getInstance(ctx: Context): SigninDataBase {
            if (instance == null) {
                instance = SigninDataBase(ctx.applicationContext)
            }
            return instance!!
        }
    }

    override fun onCreate(db: SQLiteDatabase?) {
        val drop_sql="DROP TABLE IF EXISTS ${TABLE_NAME};"
        db?.execSQL(drop_sql)
        val create_sql="CREATE TABLE IF NOT EXISTS ${TABLE_NAME}("+
                "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+
                "userName CHAR(45) NULL," + "date DATE NULL);"

        db?.execSQL(create_sql)
    }
    //添加数据
    fun insert(info:SigninData):Long{
        var result:Long=-1
        var  tempArray:List<SigninData>
        if (info.date!=null){
            val condition ="date = '${info.date}'and userName='${info.userName}'"
            tempArray=query(condition)
            Log.d("query result is","Sign in data"+tempArray.toString())
            if (tempArray.isNotEmpty()){
//                result= tempArray[0].id!!
//                Log.d("update","update id is $result")
//                info.id=result
//                update(info,condition)
                return result
            }
            else {
                val cv = ContentValues()
                cv.put("date",info.date)
                cv.put("userName",info.userName)
                use {
                    result=insert(TABLE_NAME,"",cv)
                }
                return result
            }
        }
        return result

    }
    //更新数据
    @JvmOverloads
    fun update(info: SigninData,condition: String="id=${info.id}"):Int{
        val cv = ContentValues()
        cv.put("date",info.date)
        var count=0
        use {
            count=update(TABLE_NAME,cv,condition,null)
        }
        return count
    }

    fun query(condition: String):List<SigninData>{
        val sql= "select userName,id,date from ${TABLE_NAME} where $condition;"
        Log.e("sql is",sql)
        var infoArray= mutableListOf<SigninData>()
        use {
            val cursor = rawQuery(sql,null)
            if (cursor.moveToFirst()){
                while (true){
                    val info=SigninData()
                    info.userName=cursor.getString(0)
                    info.id=cursor.getLong(1)
                    info.date= cursor.getInt(2).toString()

                    infoArray.add(info)
                    if (cursor.isLast){
                        break;
                    }
                    cursor.moveToNext()
                }
            }
            cursor.close()
        }

        return infoArray
    }


    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {

    }
}